<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客编辑页</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="./css/blogEdit.css">

    <!-- 引入 editor.md 的依赖 -->
    <link rel="stylesheet" href="editor.md/css/editormd.min.css" />
    <script src="jquery.js"></script>
    <script src="editor.md/lib/marked.min.js"></script>
    <script src="editor.md/lib/prettify.min.js"></script>
    <script src="editor.md/editormd.js"></script>
    
</head>
<body>
    <!-- 这是导航栏 -->
    <div class="nav">
        <img src="image/person_Blog.png" alt="">
        <span>我的博客系统</span>
        <!-- 空白元素, 用来占位置 -->
        <div class="spacer"></div>
        <a href="main.html">主页</a>
    </div>
    <!-- 包裹整个博客编辑页内容的顶级容器 -->
    <div class="blog-edit-container">
        <div class="title">
            <input id="title" type="text" placeholder="在此处输入标题">
            <button id="submit">发布文章</button>
        </div>
        <!-- 放置 md 编辑器 -->
        <div id="editor">
            <!--                为了进行 form的提交, 搞一个 textArea多行编辑框 -->
            <textarea id="content" style="display: none}"></textarea>
        </div>
    </div>

    <script src="js/uploadImg.js"></script>
    <script>
        // 初始化编辑器
        let editor = editormd("editor", {
            // 这里的尺寸必须在这里设置. 设置样式会被 editormd 自动覆盖掉. 
            width: "100%",
            // 设定编辑器高度
            height: "600px",
            // 编辑器中的初始内容
            markdown: "# 在这里写下一篇博客",
            // 指定 editor.md 依赖的插件路径
            path: "editor.md/lib/",
            tocDropdown: true,
            imageUpload: true,
            imageFormats:['jpg','jpeg','gif','png','bmp','webp'],
            imageUploadURL: 'blogPicture',
            onload: function () {
                // 支持复制粘贴
                initPasteDragImg(this);
            }
        });
    </script>

    <script src="jquery.js"></script>
    <script src="js/common.js"></script>
<!--    初始化文章内容 编辑功能-->
    <script>

        let title = document.querySelector("#title");
        $.ajax({
            type: 'get',
            url: 'blog' + location.search,
            success: function (body) {
                title.value = body.title;
                editor = editormd("editor", {
                    width: "100%",
                    height: "600px",
                    markdown: body.content,
                    path: "editor.md/lib/",
                    tocDropdown: true,
                    imageUpload: true,
                    imageFormats:['jpg','jpeg','gif','png','bmp','webp'],
                    imageUploadURL: 'blogPicture',
                    onload: function () {
                        // 支持复制粘贴
                        initPasteDragImg(this);
                    }
                });
            }
        })
    </script>

    <script>
        // 提交文章
        let submit = document.querySelector("#submit");
        submit.onclick = function () {
            let title = document.querySelector("#title");
            let content = document.querySelector("#content");
            $.ajax({
                type: 'post',
                url: 'blog' + location.search,
                contentType:'application/json',
                data: JSON.stringify({
                    title: title.value,
                    content: content.value
                }),
                success: function (body) {
                    if (body == 'titleNull'){
                        alert("标题不能为空!")
                    }else if (body == 'contentNull'){
                        alert("内容不能为空!")
                    }else if (body == 'success'){
                        alert("发布成功!");
                        location = 'main.html'
                    }
                }
            })
        }
    </script>
</body>
</html>